<?php

namespace App\Models\Auth;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class Authorization extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['token', 'expired_at', 'refresh_expired_at'];

    public function getPayload()
    {
        return app('tymon.jwt')->setToken($this->token)->payload();
    }

    public function toArray()
    {
        $payload = $this->getPayload();
        // get expired time
        $expiredAt        = Carbon::createFromTimestamp($payload->get('exp'))->toDateTimeString();
        $refreshExpiredAt = Carbon::createFromTimestamp($payload->get('iat'))->addMinutes(config('jwt.refresh_ttl'))->toDateTimeString();

        return [
            'token'              => $this->token,
            'expired_at'         => $expiredAt,
            'refresh_expired_at' => $refreshExpiredAt,
        ];
    }

}